<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />









  <meta name="baidu-site-verification" content="Q7ugSRSO7V" />







  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="用法单一：.                   匹配任意一个字符，如果设置 s = true，则可以匹配换行符[字符类]            匹配“字符类”中的一个字符，“字符类”见后面的说明[^字符类]           匹配“字符类”外的一个字符，“字符类”见后面的说明\小写Perl标记       匹配“Perl类”中的一个字符，“Perl类”见后面的说明\大写Perl标记">
<meta property="og:type" content="article">
<meta property="og:title" content="正则表达式">
<meta property="og:url" content="http://yoursite.com/golang/正则表达式/index.html">
<meta property="og:site_name" content="chimps">
<meta property="og:description" content="用法单一：.                   匹配任意一个字符，如果设置 s = true，则可以匹配换行符[字符类]            匹配“字符类”中的一个字符，“字符类”见后面的说明[^字符类]           匹配“字符类”外的一个字符，“字符类”见后面的说明\小写Perl标记       匹配“Perl类”中的一个字符，“Perl类”见后面的说明\大写Perl标记">
<meta property="og:updated_time" content="2017-04-23T13:42:26.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="正则表达式">
<meta name="twitter:description" content="用法单一：.                   匹配任意一个字符，如果设置 s = true，则可以匹配换行符[字符类]            匹配“字符类”中的一个字符，“字符类”见后面的说明[^字符类]           匹配“字符类”外的一个字符，“字符类”见后面的说明\小写Perl标记       匹配“Perl类”中的一个字符，“Perl类”见后面的说明\大写Perl标记">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"hide","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":30},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/golang/正则表达式/"/>





  <title> 正则表达式 | chimps </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?dcb994042608bdb310eff950ba755ff1";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">chimps</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/golang/正则表达式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="chimps">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://sfault-avatar.b0.upaiyun.com/416/326/4163260007-58a1621e63a68_huge256">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="chimps">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                正则表达式
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-23T21:42:26+08:00">
                2017-04-23
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/golang/" itemprop="url" rel="index">
                    <span itemprop="name">golang</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a class="cloud-tie-join-count" href="/golang/正则表达式/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count join-count" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="用法"><a href="#用法" class="headerlink" title="用法"></a>用法</h1><h2 id="单一："><a href="#单一：" class="headerlink" title="单一："></a>单一：</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">.                   匹配任意一个字符，如果设置 s = true，则可以匹配换行符</div><div class="line"></div><div class="line">[字符类]            匹配“字符类”中的一个字符，“字符类”见后面的说明</div><div class="line">[^字符类]           匹配“字符类”外的一个字符，“字符类”见后面的说明</div><div class="line"></div><div class="line">\小写Perl标记       匹配“Perl类”中的一个字符，“Perl类”见后面的说明</div><div class="line">\大写Perl标记       匹配“Perl类”外的一个字符，“Perl类”见后面的说明</div><div class="line"></div><div class="line">[:ASCII类名:]       匹配“ASCII类”中的一个字符，“ASCII类”见后面的说明</div><div class="line">[:^ASCII类名:]      匹配“ASCII类”外的一个字符，“ASCII类”见后面的说明</div><div class="line"></div><div class="line">\pUnicode普通类名   匹配“Unicode类”中的一个字符(仅普通类)，“Unicode类”见后面的说明</div><div class="line">\PUnicode普通类名   匹配“Unicode类”外的一个字符(仅普通类)，“Unicode类”见后面的说明</div><div class="line"></div><div class="line">\p&#123;Unicode类名&#125;     匹配“Unicode类”中的一个字符，“Unicode类”见后面的说明</div><div class="line">\P&#123;Unicode类名&#125;     匹配“Unicode类”外的一个字符，“Unicode类”见后面的说明</div></pre></td></tr></table></figure>
<h2 id="复合"><a href="#复合" class="headerlink" title="复合"></a>复合</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">xy             匹配 xy（x 后面跟随 y）</div><div class="line">x|y            匹配 x 或 y (优先匹配 x)</div></pre></td></tr></table></figure>
<h2 id="重复"><a href="#重复" class="headerlink" title="重复"></a>重复</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">x*             匹配零个或多个 x，优先匹配更多(贪婪)</div><div class="line">x+             匹配一个或多个 x，优先匹配更多(贪婪)</div><div class="line">x?             匹配零个或一个 x，优先匹配一个(贪婪)</div><div class="line">x&#123;n,m&#125;         匹配 n 到 m 个 x，优先匹配更多(贪婪)</div><div class="line">x&#123;n,&#125;          匹配 n 个或多个 x，优先匹配更多(贪婪)</div><div class="line">x&#123;n&#125;           只匹配 n 个 x</div><div class="line">x*?            匹配零个或多个 x，优先匹配更少(非贪婪)</div><div class="line">x+?            匹配一个或多个 x，优先匹配更少(非贪婪)</div><div class="line">x??            匹配零个或一个 x，优先匹配零个(非贪婪)</div><div class="line">x&#123;n,m&#125;?        匹配 n 到 m 个 x，优先匹配更少(非贪婪)</div><div class="line">x&#123;n,&#125;?         匹配 n 个或多个 x，优先匹配更少(非贪婪)</div><div class="line">x&#123;n&#125;?          只匹配 n 个 x</div></pre></td></tr></table></figure>
<h2 id="分组"><a href="#分组" class="headerlink" title="分组"></a>分组</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">(子表达式)            被捕获的组，该组被编号 (子匹配)</div><div class="line">(?P&lt;命名&gt;子表达式)    被捕获的组，该组被编号且被命名 (子匹配)</div><div class="line">(?:子表达式)          非捕获的组 (子匹配)</div><div class="line">(?标记)               在组内设置标记，非捕获，标记影响当前组后的正则表达式</div><div class="line">(?标记:子表达式)      在组内设置标记，非捕获，标记影响当前组内的子表达式</div><div class="line"></div><div class="line">标记的语法是：</div><div class="line">xyz  (设置 xyz 标记)</div><div class="line">-xyz (清除 xyz 标记)</div><div class="line">xy-z (设置 xy 标记, 清除 z 标记)</div><div class="line"></div><div class="line">可以设置的标记有：</div><div class="line">i              不区分大小写 (默认为 false)</div><div class="line">m              多行模式：让 ^ 和 $ 匹配整个文本的开头和结尾，而非行首和行尾(默认为 false)</div><div class="line">s              让 . 匹配 \n (默认为 false)</div><div class="line">U              非贪婪模式：交换 x* 和 x*? 等的含义 (默认为 false)</div></pre></td></tr></table></figure>
<h2 id="位置标记："><a href="#位置标记：" class="headerlink" title="位置标记："></a>位置标记：</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">^              如果标记 m=true 则匹配行首，否则匹配整个文本的开头（m 默认为 false）</div><div class="line">$              如果标记 m=true 则匹配行尾，否则匹配整个文本的结尾（m 默认为 false）</div><div class="line">\A             匹配整个文本的开头，忽略 m 标记</div><div class="line">\b             匹配单词边界</div><div class="line">\B             匹配非单词边界</div><div class="line">\z             匹配整个文本的结尾，忽略 m 标记</div></pre></td></tr></table></figure>
<h2 id="转序标记"><a href="#转序标记" class="headerlink" title="转序标记"></a>转序标记</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">\a             匹配响铃符    （相当于 \x07）</div><div class="line">               注意：正则表达式中不能使用 \b 匹配退格符，因为 \b 被用来匹配单词边界，</div><div class="line">               可以使用 \x08 表示退格符。</div><div class="line">\f             匹配换页符    （相当于 \x0C）</div><div class="line">\t             匹配横向制表符（相当于 \x09）</div><div class="line">\n             匹配换行符    （相当于 \x0A）</div><div class="line">\r             匹配回车符    （相当于 \x0D）</div><div class="line">\v             匹配纵向制表符（相当于 \x0B）</div><div class="line">\123           匹配 8  進制编码所代表的字符（必须是 3 位数字）</div><div class="line">\x7F           匹配 16 進制编码所代表的字符（必须是 3 位数字）</div><div class="line">\x&#123;10FFFF&#125;     匹配 16 進制编码所代表的字符（最大值 10FFFF  ）</div><div class="line">\Q...\E        匹配 \Q 和 \E 之间的文本，忽略文本中的正则语法</div><div class="line"></div><div class="line">\\             匹配字符 \</div><div class="line">\^             匹配字符 ^</div><div class="line">\$             匹配字符 $</div><div class="line">\.             匹配字符 .</div><div class="line">\*             匹配字符 *</div><div class="line">\+             匹配字符 +</div><div class="line">\?             匹配字符 ?</div><div class="line">\&#123;             匹配字符 &#123;</div><div class="line">\&#125;             匹配字符 &#125;</div><div class="line">\(             匹配字符 (</div><div class="line">\)             匹配字符 )</div><div class="line">\[             匹配字符 [</div><div class="line">\]             匹配字符 ]</div><div class="line">\|             匹配字符 |</div></pre></td></tr></table></figure>
<h2 id="可以将“命名字符类”作为“字符类”的元素："><a href="#可以将“命名字符类”作为“字符类”的元素：" class="headerlink" title="可以将“命名字符类”作为“字符类”的元素："></a>可以将“命名字符类”作为“字符类”的元素：</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">[\d]           匹配数字 (相当于 \d)</div><div class="line">[^\d]          匹配非数字 (相当于 \D)</div><div class="line">[\D]           匹配非数字 (相当于 \D)</div><div class="line">[^\D]          匹配数字 (相当于 \d)</div><div class="line">[[:name:]]     命名的“ASCII 类”包含在“字符类”中 (相当于 [:name:])</div><div class="line">[^[:name:]]    命名的“ASCII 类”不包含在“字符类”中 (相当于 [:^name:])</div><div class="line">[\p&#123;Name&#125;]     命名的“Unicode 类”包含在“字符类”中 (相当于 \p&#123;Name&#125;)</div><div class="line">[^\p&#123;Name&#125;]    命名的“Unicode 类”不包含在“字符类”中 (相当于 \P&#123;Name&#125;)</div></pre></td></tr></table></figure>
<h1 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h1><p><code>字符类</code>取值如下（<code>字符类</code>包含<code>Perl类</code>、<code>ASCII类</code>、<code>Unicode类</code>）： x 单个字符 A-Z 字符范围(包含首尾字符) \小写字母 Perl类 [:ASCII类名:] ASCII类 \p{Unicode脚本类名} Unicode类 (脚本类) \pUnicode普通类名 Unicode类 (普通类)</p>
<h2 id="“Perl-类”取值如下："><a href="#“Perl-类”取值如下：" class="headerlink" title="“Perl 类”取值如下："></a>“Perl 类”取值如下：</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">\d             数字 (相当于 [0-9])</div><div class="line">\D             非数字 (相当于 [^0-9])</div><div class="line">\s             空白 (相当于 [\t\n\f\r ])</div><div class="line">\S             非空白 (相当于[^\t\n\f\r ])</div><div class="line">\w             单词字符 (相当于 [0-9A-Za-z_])</div><div class="line">\W             非单词字符 (相当于 [^0-9A-Za-z_])</div></pre></td></tr></table></figure>
<h2 id="“ASCII-类”取值如下"><a href="#“ASCII-类”取值如下" class="headerlink" title="“ASCII 类”取值如下"></a>“ASCII 类”取值如下</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">[:alnum:]      字母数字 (相当于 [0-9A-Za-z])</div><div class="line">[:alpha:]      字母 (相当于 [A-Za-z])</div><div class="line">[:ascii:]      ASCII 字符集 (相当于 [\x00-\x7F])</div><div class="line">[:blank:]      空白占位符 (相当于 [\t ])</div><div class="line">[:cntrl:]      控制字符 (相当于 [\x00-\x1F\x7F])</div><div class="line">[:digit:]      数字 (相当于 [0-9])</div><div class="line">[:graph:]      图形字符 (相当于 [!-~] 相当于 [A-Za-z0-9!&quot;# $%&amp;&apos;()*+,\-./:;&lt;=&gt;?@[\\\]^_`&#123;|&#125;~])</div><div class="line">[:lower:]      小写字母 (相当于 [a-z])</div><div class="line">[:print:]      可打印字符 (相当于 [ -~] 相当于 [ [:graph:]])</div><div class="line">[:punct:]      标点符号 (相当于 [!-/:-@[-`&#123;-~])</div><div class="line">[:space:]      空白字符(相当于 [\t\n\v\f\r ])</div><div class="line">[:upper:]      大写字母(相当于 [A-Z])</div><div class="line">[:word:]       单词字符(相当于 [0-9A-Za-z_])</div><div class="line">[:xdigit:]     16 進制字符集(相当于 [0-9A-Fa-f])</div></pre></td></tr></table></figure>
<h2 id="“Unicode-类”取值如下—普通类："><a href="#“Unicode-类”取值如下—普通类：" class="headerlink" title="“Unicode 类”取值如下—普通类："></a>“Unicode 类”取值如下—普通类：</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">C                 -其他-          (other)</div><div class="line">Cc                控制字符        (control)</div><div class="line">Cf                格式            (format)</div><div class="line">Co                私人使用区      (private use)</div><div class="line">Cs                代理区          (surrogate)</div><div class="line">L                 -字母-          (letter)</div><div class="line">Ll                小写字母        (lowercase letter)</div><div class="line">Lm                修饰字母        (modifier letter)</div><div class="line">Lo                其它字母        (other letter)</div><div class="line">Lt                首字母大写字母  (titlecase letter)</div><div class="line">Lu                大写字母        (uppercase letter)</div><div class="line">M                 -标记-          (mark)</div><div class="line">Mc                间距标记        (spacing mark)</div><div class="line">Me                关闭标记        (enclosing mark)</div><div class="line">Mn                非间距标记      (non-spacing mark)</div><div class="line">N                 -数字-          (number)</div><div class="line">Nd                十進制数字      (decimal number)</div><div class="line">Nl                字母数字        (letter number)</div><div class="line">No                其它数字        (other number)</div><div class="line">P                 -标点-          (punctuation)</div><div class="line">Pc                连接符标点      (connector punctuation)</div><div class="line">Pd                破折号标点符号  (dash punctuation)</div><div class="line">Pe                关闭的标点符号  (close punctuation)</div><div class="line">Pf                最后的标点符号  (final punctuation)</div><div class="line">Pi                最初的标点符号  (initial punctuation)</div><div class="line">Po                其他标点符号    (other punctuation)</div><div class="line">Ps                开放的标点符号  (open punctuation)</div><div class="line">S                 -符号-          (symbol)</div><div class="line">Sc                货币符号        (currency symbol)</div><div class="line">Sk                修饰符号        (modifier symbol)</div><div class="line">Sm                数学符号        (math symbol)</div><div class="line">So                其他符号        (other symbol)</div><div class="line">Z                 -分隔符-        (separator)</div><div class="line">Zl                行分隔符        (line separator)</div><div class="line">Zp                段落分隔符      (paragraph separator)</div><div class="line">Zs                空白分隔符      (space separator)</div></pre></td></tr></table></figure>
<h2 id="“Unicode-类”取值如下—脚本类："><a href="#“Unicode-类”取值如下—脚本类：" class="headerlink" title="“Unicode 类”取值如下—脚本类："></a>“Unicode 类”取值如下—脚本类：</h2><figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">Arabic                  阿拉伯文</div><div class="line">Armenian                亚美尼亚文</div><div class="line">Balinese                巴厘岛文</div><div class="line">Bengali                 孟加拉文</div><div class="line">Bopomofo                汉语拼音字母</div><div class="line">Braille                 盲文</div><div class="line">Buginese                布吉文</div><div class="line">Buhid                   布希德文</div><div class="line">Canadian_Aboriginal     加拿大土著文</div><div class="line">Carian                  卡里亚文</div><div class="line">Cham                    占族文</div><div class="line">Cherokee                切诺基文</div><div class="line">Common                  普通的，字符不是特定于一个脚本</div><div class="line">Coptic                  科普特文</div><div class="line">Cuneiform               楔形文字</div><div class="line">Cypriot                 塞浦路斯文</div><div class="line">Cyrillic                斯拉夫文</div><div class="line">Deseret                 犹他州文</div><div class="line">Devanagari              梵文</div><div class="line">Ethiopic                衣索比亚文</div><div class="line">Georgian                格鲁吉亚文</div><div class="line">Glagolitic              格拉哥里文</div><div class="line">Gothic                  哥特文</div><div class="line">Greek                   希腊</div><div class="line">Gujarati                古吉拉特文</div><div class="line">Gurmukhi                果鲁穆奇文</div><div class="line">Han                     汉文</div><div class="line">Hangul                  韩文</div><div class="line">Hanunoo                 哈鲁喏文</div><div class="line">Hebrew                  希伯来文</div><div class="line">Hiragana                平假名（日语）</div><div class="line">Inherited               继承前一个字符的脚本</div><div class="line">Kannada                 坎那达文</div><div class="line">Katakana                片假名（日语）</div><div class="line">Kayah_Li                克耶字母</div><div class="line">Kharoshthi              卡罗须提文</div><div class="line">Khmer                   高棉文</div><div class="line">Lao                     老挝文</div><div class="line">Latin                   拉丁文</div><div class="line">Lepcha                  雷布查文</div><div class="line">Limbu                   林布文</div><div class="line">Linear_B                B类线形文字（古希腊）</div><div class="line">Lycian                  利西亚文</div><div class="line">Lydian                  吕底亚文</div><div class="line">Malayalam               马拉雅拉姆文</div><div class="line">Mongolian               蒙古文</div><div class="line">Myanmar                 缅甸文</div><div class="line">New_Tai_Lue             新傣仂文</div><div class="line">Nko                     Nko文</div><div class="line">Ogham                   欧甘文</div><div class="line">Ol_Chiki                桑塔利文</div><div class="line">Old_Italic              古意大利文</div><div class="line">Old_Persian             古波斯文</div><div class="line">Oriya                   奥里亚文</div><div class="line">Osmanya                 奥斯曼亚文</div><div class="line">Phags_Pa                八思巴文</div><div class="line">Phoenician              腓尼基文</div><div class="line">Rejang                  拉让文</div><div class="line">Runic                   古代北欧文字</div><div class="line">Saurashtra              索拉什特拉文（印度县城）</div><div class="line">Shavian                 萧伯纳文</div><div class="line">Sinhala                 僧伽罗文</div><div class="line">Sundanese               巽他文</div><div class="line">Syloti_Nagri            锡尔赫特文</div><div class="line">Syriac                  叙利亚文</div><div class="line">Tagalog                 塔加拉文</div><div class="line">Tagbanwa                塔格巴努亚文</div><div class="line">Tai_Le                  德宏傣文</div><div class="line">Tamil                   泰米尔文</div><div class="line">Telugu                  泰卢固文</div><div class="line">Thaana                  塔安那文</div><div class="line">Thai                    泰文</div><div class="line">Tibetan                 藏文</div><div class="line">Tifinagh                提非纳文</div><div class="line">Ugaritic                乌加里特文</div><div class="line">Vai                     瓦伊文</div><div class="line">Yi                      彝文</div></pre></td></tr></table></figure>
<h2 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h2><p>　　对于 [a-z] 这样的正则表达式，如果要在 [] 中匹配 - ，可以将 - 放在 [] 的开头或结尾，例如 [-a-z] 或 [a-z-]</p>
<p>　　可以在 [] 中使用转义字符：\f、\t、\n、\r、\v、\377、\xFF、\x{10FFFF}、\、\^、\$、.、*、+、\?、{、}、(、)、[、]、|（具体含义见上面的说明）</p>
<p>　　如果在正则表达式中使用了分组，则在执行正则替换的时候，“替换内容”中可以使用 $1、${1}、$name、${name} 这样的“分组引用符”获取相应的分组内容。其中 $0 代表整个匹配项，$1 代表第 1 个分组，$2 代表第 2 个分组，……。</p>
<p>　　如果“分组引用符”是 $name 的形式，则在解析的时候，name 是取尽可能长的字符串，比如：$1x 相当于 ${1x}，而不是${1}x，再比如：$10 相当于 ${10}，而不是 ${1}0。</p>
<p>　　由于 $ 字符会被转义，所以要在“替换内容”中使用 $ 字符，可以用 \$ 代替。</p>
<p>　　上面介绍的正则表达式语法是“Perl 语法”，除了“Perl 语法”外，Go 语言中还有另一种“POSIX 语法”，“POSIX 语法”除了不能使用“Perl 类”之外，其它都一样。</p>
<h1 id="示例："><a href="#示例：" class="headerlink" title="示例："></a>示例：</h1><p>func main() { text := Hello 世界！123 Go.<br><figure class="highlight go"><table><tr><td class="code"><pre><div class="line"><span class="comment">// 查找连续的小写字母</span></div><div class="line">reg := regexp.MustCompile(<span class="string">`[a-z]+`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["ello" "o"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找连续的非小写字母</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`[^a-z]+`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["H" " 世界！123 G" "."]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找连续的单词字母</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`[\w]+`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello" "123" "Go"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找连续的非单词字母、非空白字符</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`[^\w\s]+`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["世界！" "."]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找连续的大写字母</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`[[:upper:]]+`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["H" "G"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找连续的非 ASCII 字符</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`[[:^ascii:]]+`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["世界！"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找连续的标点符号</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`[\pP]+`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["！" "."]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找连续的非标点符号字符</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`[\PP]+`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello 世界" "123 Go"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找连续的汉字</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`[\p&#123;Han&#125;]+`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["世界"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找连续的非汉字字符</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`[\P&#123;Han&#125;]+`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello " "！123 Go."]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找 Hello 或 Go</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`Hello|Go`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello" "Go"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找行首以 H 开头，以空格结尾的字符串</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`^H.*\s`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello 世界！123 "]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找行首以 H 开头，以空白结尾的字符串（非贪婪模式）</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`(?U)^H.*\s`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello "]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找以 hello 开头（忽略大小写），以 Go 结尾的字符串</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`(?i:^hello).*Go`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello 世界！123 Go"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找 Go.</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`\QGo.\E`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Go."]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找从行首开始，以空格结尾的字符串（非贪婪模式）</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`(?U)^.* `</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello "]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找以空格开头，到行尾结束，中间不包含空格字符串</span></div><div class="line">reg = regexp.MustCompile(<span class="string">` [^ ]*$`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// [" Go."]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找“单词边界”之间的字符串</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`(?U)\b.+\b`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello" " 世界！" "123" " " "Go"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找连续 1 次到 4 次的非空格字符，并以 o 结尾的字符串</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`[^ ]&#123;1,4&#125;o`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello" "Go"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找 Hello 或 Go</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`(?:Hell|G)o`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.FindAllString(text, <span class="number">-1</span>))</div><div class="line"><span class="comment">// ["Hello" "Go"]</span></div><div class="line"></div><div class="line"><span class="comment">// 查找 Hello 或 Go，替换为 Hellooo、Gooo</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`(?PHell|G)o`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.ReplaceAllString(text, <span class="string">"$&#123;n&#125;ooo"</span>))</div><div class="line"><span class="comment">// "Hellooo 世界！123 Gooo."</span></div><div class="line"></div><div class="line"><span class="comment">// 交换 Hello 和 Go</span></div><div class="line">reg = regexp.MustCompile(<span class="string">`(Hello)(.*)(Go)`</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.ReplaceAllString(text, <span class="string">"$3$2$1"</span>))</div><div class="line"><span class="comment">// "Go 世界！123 Hello."</span></div><div class="line"></div><div class="line"><span class="comment">// 特殊字符的查找</span></div><div class="line">reg = regexp.MustCompile(<span class="string">"[\\f\\t\\n\\r\\v\\123\\x7F\\x&#123;10FFFF&#125;\\\\\\^\\$\\.\\*\\+\\?\\&#123;\\&#125;\\(\\)\\[\\]\\|]"</span>)</div><div class="line">fmt.Printf(<span class="string">"%q\n"</span>, reg.ReplaceAllString(<span class="string">"\f\t\n\r\v\123\x7F\U0010FFFF\\^$.*+?&#123;&#125;()[]|"</span>, <span class="string">"-"</span>))</div><div class="line"><span class="comment">// "----------------------"</span></div></pre></td></tr></table></figure></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/linux/pdf处理/" rel="next" title="pdf处理">
                <i class="fa fa-chevron-left"></i> pdf处理
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/linux/Awk/" rel="prev" title="Awk">
                Awk <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="cloud-tie-wrapper" class="cloud-tie-wrapper"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="https://sfault-avatar.b0.upaiyun.com/416/326/4163260007-58a1621e63a68_huge256"
               alt="chimps" />
          <p class="site-author-name" itemprop="name">chimps</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">59</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">12</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">19</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#用法"><span class="nav-number">1.</span> <span class="nav-text">用法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#单一："><span class="nav-number">1.1.</span> <span class="nav-text">单一：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#复合"><span class="nav-number">1.2.</span> <span class="nav-text">复合</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#重复"><span class="nav-number">1.3.</span> <span class="nav-text">重复</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分组"><span class="nav-number">1.4.</span> <span class="nav-text">分组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#位置标记："><span class="nav-number">1.5.</span> <span class="nav-text">位置标记：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#转序标记"><span class="nav-number">1.6.</span> <span class="nav-text">转序标记</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#可以将“命名字符类”作为“字符类”的元素："><span class="nav-number">1.7.</span> <span class="nav-text">可以将“命名字符类”作为“字符类”的元素：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#说明"><span class="nav-number">2.</span> <span class="nav-text">说明</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#“Perl-类”取值如下："><span class="nav-number">2.1.</span> <span class="nav-text">“Perl 类”取值如下：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#“ASCII-类”取值如下"><span class="nav-number">2.2.</span> <span class="nav-text">“ASCII 类”取值如下</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#“Unicode-类”取值如下—普通类："><span class="nav-number">2.3.</span> <span class="nav-text">“Unicode 类”取值如下—普通类：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#“Unicode-类”取值如下—脚本类："><span class="nav-number">2.4.</span> <span class="nav-text">“Unicode 类”取值如下—脚本类：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#注意"><span class="nav-number">2.5.</span> <span class="nav-text">注意</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#示例："><span class="nav-number">3.</span> <span class="nav-text">示例：</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">chimps</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  








  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  


  




	





  
    
    <script>
      var cloudTieConfig = {
        url: document.location.href, 
        sourceId: "",
        productKey: "48bb30d8783d4b38ba0c95e0283238ca",
        target: "cloud-tie-wrapper"
      };
    </script>
    <script src="https://img1.ws.126.net/f2e/tie/yun/sdk/loader.js"></script>
  










  





  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  

  

  

</body>
</html>
